package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.WorkflowNodeMapper;
import com.zhentao.pojo.WorkflowNode;
import com.zhentao.service.WorkflowNodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程节点Service实现类
 */
@Service
public class WorkflowNodeServiceImpl extends ServiceImpl<WorkflowNodeMapper, WorkflowNode> implements WorkflowNodeService {
    
    @Override
    public List<WorkflowNode> getNodesByProcessId(Long processId) {
        return baseMapper.selectByProcessId(processId);
    }
    
    @Override
    @Transactional
    public boolean saveNodes(Long processId, List<WorkflowNode> nodes) {
        // 先删除原有节点
        QueryWrapper<WorkflowNode> wrapper = new QueryWrapper<>();
        wrapper.eq("process_id", processId);
        baseMapper.delete(wrapper);
        
        // 保存新节点
        if (nodes != null && !nodes.isEmpty()) {
            for (int i = 0; i < nodes.size(); i++) {
                WorkflowNode node = nodes.get(i);
                node.setProcessId(processId);
                node.setSort(i + 1);
                baseMapper.insert(node);
            }
        }
        return true;
    }
} 